package TUILayer;//makes this class a part of the TUILayer package
import java.util.Scanner;
import ControlLayer.*;//The TUILayer needs to know the ControlLayer
import ModelLayer.*;
 

/**
 * User interface class to the AddressBook
 * 
 * @author (kbh) 
 * @version (2005.03.31)
 * (FEN): 2006.03.21: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 */
public class AddressBookUI
{   
    private AddressCtr addrCtr;//reference to the ControlLayer
    private AddressBook adrB;
    
    //constructor which does nothing
    public AddressBookUI()
    {
       addrCtr = new AddressCtr();
       adrB = new AddressBook();
    }
    
    public void start()
    {
        addressMenu();
    }
    
    public void addressMenu()
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = writeAddressBookMenu(); 
          if(choice == 1)
          {   //show person
              getPerson();
          }
          else
          {
              if(choice == 2)
              {
                    //create person
                     createPerson();
              }  
              else
              {
                  if(choice == 3)
                  {
                      //delete person
                      deletePerson();
                  }  
                  else
                  {
                      if(choice == 4)
                      {
                          // update person
                          updatePerson();
                      }
                      else
                      {
                          if(choice == 5)
                          {
                              listAllPersons();
                          }
                          else
                          {
                              exit = true;
                          }//end else
                      }//end else
                  }//end else
              }//end else
          }//end else
       }//end while
    }
    
    public int writeAddressBookMenu()
    {      // makes an object keyboard to read input from the screen
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** AddressBook Menu ***");
            System.out.println(" (1) Show information about person");
            System.out.println(" (2) Add person");
            System.out.println(" (3) Delete person");
            System.out.println(" (4) Update person");
            System.out.println(" (5) List all persons");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
        
    public int updatePersonMenu()
    {    // makes an object keyboard to read input from the screen
          Scanner keyboard = new Scanner(System.in);
          System.out.println("\f *** Update Menu ***");
          System.out.println(" (1) Update Name");
          System.out.println(" (2) Update Address");
          System.out.println(" (3) Update Postal Code");
          System.out.println(" (4) Update City");
          System.out.println(" (5) Update phone");
          System.out.println(" (6) Return");
          System.out.print("\n Make your choice: ");
            
          int choice = keyboard.nextInt();
          return choice;
    }
        
    public void updateMenu(Person p)
    {
       boolean exit = false;
       while(!exit)//that is: while exit is false
       {
          int choice = updatePersonMenu(); 
          if(choice == 1)
          {   
              updatePersonName(p);
          }
          else
          {
              if(choice == 2)
              {
                     updatePersonAddress(p);
              }  
              else
              {
                  if(choice == 3)
                  {
                      updatePersonPostalCode(p);
                  }  
                  else
                  {
                      if(choice == 4)
                      {
                          updatePersonCity(p);
                      }
                      else
                      {
                          if(choice == 5)
                          {
                              updatePersonPhone(p);
                          }
                          else
                          {
                              exit = true;
                          }//end else
                      }//end else
                  }//end else
              }//end else
          }//end else
       }//end while
    }
    
    //the method reads id for a person and return
    // 
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write Id:  ");
           try
           {
             id = keyboard.nextLong();
             ok = true;
           }
           catch (Exception e)
           {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
   
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    }
    
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
     
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
    
    public void showName(String name)
    { 
        System.out.println("The name of the persone: " + name);
    }
    
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }
    
    public void showPostalCode(String postalCode)
    { 
        System.out.println("The postal code of the person: " + postalCode);
    }
    
    public void showCity(String city)
    { 
        System.out.println("The city of the person: " + city);
    }
    
    public void showPhone(String phone)
    { 
        System.out.println("The phone of the person: " + phone);
    }

    private void getPerson()
    {//is to be changed when the address book controller is completed
        long id = inputPersonsID();
    }  
    
    private void createPerson()
    {
        long id = inputPersonsID();
        if(addrCtr.getPerson(id) == null)
        {
            String name = inputName();
            String address = inputAddress();
            String postalCode = inputPostalCode();
            String city = inputCity();
            String phone = inputPhone();
            addrCtr.createPerson(id, name, address, postalCode, city, phone);
        }
        else
        {
            System.out.println("Id already exists in the list");
            pause();
        }
    }
    
    private void deletePerson()
    {
        long id = inputPersonsID();
        if (addrCtr.getPerson(id)!=null) 
        {
            addrCtr.deletePerson(id);
        }
        else 
        {
            System.out.println("no person found");
            pause();
        }
    }
    
    private void updatePerson()
    {
        long id = inputPersonsID();
        if ( addrCtr.getPerson(id)!=null)
        {
            Person p = addrCtr.getPerson(id);
            updateMenu(p);
        }
        else
        {
            System.out.println("no person found");
            pause();
        }
    }
    
    private void updatePersonName(Person p)
    {
        showName(p.getName());
        p.setName(inputName()); 
    }
    
    private void updatePersonAddress(Person p)
    {
        showAddress(p.getAddress());
        p.setAddress(inputAddress()); 
    }

    private void updatePersonPostalCode(Person p)
    {
        showPostalCode(p.getPostalCode());
        p.setPostalCode(inputPostalCode()); 
    }
    
    private void updatePersonCity(Person p)
    {
        showCity(p.getCity());
        p.setCity(inputCity());
    }
    
    private void updatePersonPhone(Person p)
    {
        showPhone(p.getPhone());
        p.setPhone(inputPhone());
    }
    
    private void listAllPersons()
    {
        addrCtr.listAllPersons();
        pause();
    }
    
    private void pause()
    {
         Scanner keyboard = new Scanner(System.in);
         System.out.println("Enter to continue");
         String address = keyboard.nextLine();
    }
}
            
        
